import React from 'react';
import { Navigate, useLocation } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { RootState } from '../../store';
import AuthWrapper from './AuthWrapper';

interface AuthRouteProps {
    children: React.ReactNode;
}

const AuthRoute: React.FC<AuthRouteProps> = ({ children }) => {
    const { token } = useSelector((state: RootState) => state.auth);
    const location = useLocation();

    if (!token) {
        // 未登录，重定向到登录页
        return <Navigate to="/login" state={{ from: location }} replace />;
    }

    // 已登录，渲染子组件
    return <>{children}</>;
};

export default AuthRoute;
export { AuthWrapper };